<?php
/*
 * File statistics.php for project Rijlesplanner.nl
 * Created on 1 dec 2007 at 21:02:10 by Ivar Pruijn
 */
class statistics extends modules {

	// Properties:
	protected $name = "statistics";
	protected $title = "Statistieken";
	protected $subtitle = "";
	protected $moduleOptions = array ();

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for this class.
	 */
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
		$this->wsclient = parent :: getWsclient();
	}

	/**
	 * Object process()
	 * 
	 * Processes the template and current module
	 * 
	 * @param Smarty template The Smarty template to process (sets variables etc.)
	 * @return Smarty The template that has been processed
	 */
	function process(& $template = null) {
		$this->template = $template;
		if (!empty ($_POST)) {
			switch ($_POST['action']) {
				default :
					if (in_array($this->name, $this->user->getAcl())) {
						$this->getStatistics();
					} else {
						$this->actionAccessDenied();
					}
					$this->template->assign('moduleTemplate', 'modules/statistics/statistics.tpl');
					break;
			}
		} else {
			$this->getStatistics();
			$this->template->assign('moduleTemplate', 'modules/statistics/statistics.tpl');
		}
	}

	/**
	 * boolean getStatistics()
	 * 
	 * Retrieves the statistics
	 * Sets an error array in the template in case of errors.
	 */
	function getStatistics() {
		$errors = array();
		$statistics = array();
		try {
			/*
			$result = $this->wsclient->WS_Beoordelingsberichten(array (
				'Handle' => $_POST['rijschool']
			));
			$this->logger->log("Retrieved the following statistics:" . $result->Statistieken . "!", PEAR_LOG_DEBUG);
			*/
			$statistics = array('number_passed'=>7654,'number_active'=>1234,'average_pass_percentage'=>'76,54');
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_DEBUG);
			switch ($e->faultcode) {
				default :
					$errorcode = 70;
					$errormessage = 'Onbekende fout bij ophalen statistieken'; 
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}

		$this->template->assign('statistics', $statistics);
		$this->template->assign('errors', $errors);
	}

	public function getTitle() {
	  return $this->title;
	}

	public function getSubTitle() {
		if(!empty($_GET['option'])) {
			$subtitle = "";
	  		foreach($this->moduleOptions AS $mOpt) {
	  			if($mOpt['name'] == $_GET['option']) {
	  				$subtitle = $mOpt['title'];
	  			}
	  		}
	  		return $subtitle;
		} else {
	  		return $this->subtitle;
	  	}
	}

	public function setSubTitle($newVal="") {
		if(!empty($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	 * string __toString()
	 * 
	 * Returns a String representation of this class.
	 * 
	 * @return string A representation of this class
	 */
	public function __toString() {
		return $this->title;
	}
}
?>